package com.heiyu.api.controller;

import com.google.common.collect.Lists;
import com.heiyu.api.request.SearchListRequest;
import com.heiyu.api.response.SearchGetResponse;
import com.heiyu.api.response.SearchListResponse;
import com.heiyu.api.response.model.SearchGetModel;
import com.heiyu.api.response.model.SearchListInfo;
import com.heiyu.api.response.model.SearchListModel;
import com.heiyu.api.response.model.TargetInfoModel;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.*;
import com.heiyu.entity.*;
import com.heiyu.utils.MarketSupportTypeUtils;
import com.heiyu.utils.MoneyUtils;
import com.heiyu.utils.PageUtils;
import com.heiyu.utils.RangeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by mountain on 18/6/11.
 */
@Api(value = "搜索相关",description = "搜索相关")
@RestController
@RequestMapping("api/v1/search")
public class SearchController extends ApiBaseController{

    @Autowired
    private GlobalShowInfoMapper globalShowInfoMapper;
    @Autowired
    private MarketBaseInfoMapper marketBaseInfoMapper;
    @Autowired
    private ShowInfoMapper showInfoMapper;
    @Autowired
    private UserFollowMapper userFollowMapper;
    @Autowired
    private MarketCoinCoinMapper marketCoinCoinMapper;

    @ApiOperation(value = "get",notes = "搜索页面获取数据")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public SearchGetResponse get(){
        SearchGetResponse response = new SearchGetResponse();
        response.success();
        Map<String,Object> param = PageUtils.newParam(1,10);
        param.put("tradePriceDesc","desc");
        List<GlobalShowInfo> hoteCoinList = globalShowInfoMapper.selectByOrderDesc(param);
        SearchGetModel model = new SearchGetModel();
        model.setHotCoinList(Lists.newArrayList());
        if(CollectionUtils.isNotEmpty(hoteCoinList)){
            TargetInfoModel entity;
            for(GlobalShowInfo info:hoteCoinList){
                entity = new TargetInfoModel();
                entity.setName(info.getSymbol());
                entity.setTargetId(info.getTargetId());
                entity.setFullName(info.getUsName());
                model.getHotCoinList().add(entity);
            }
        }
        param = PageUtils.newParam(1,5);
        List<MarketBaseInfo> list = this.marketBaseInfoMapper.selectForCon(param);
        model.setHotMarketList(Lists.newArrayList());
        if(CollectionUtils.isNotEmpty(list)){
            TargetInfoModel entity;
            for(MarketBaseInfo baseInfo:list){
                entity = new TargetInfoModel();
                entity.setTargetId(baseInfo.getMarketName());
                entity.setName(baseInfo.getMarketName());
                model.getHotMarketList().add(entity);
            }
        }
        response.setData(model);
        return response;
    }

    @ApiOperation(value = "list",notes = "获取搜索数据")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public SearchListResponse list(@ModelAttribute("request")SearchListRequest request){
        SearchListResponse response = new SearchListResponse();
        response.success();
        if(StringUtils.isEmpty(request.getType())){
            request.setType("01");
        }
        response.setData(new SearchListModel());
        response.getData().setList(Lists.newArrayList());
        SearchListInfo info;
        UserInfo  userInfo = getUser();
        switch (request.getType()){
            case "01":
               List<GlobalShowInfo> list =  globalShowInfoMapper.search(request.getKeyword());
               if(CollectionUtils.isNotEmpty(list)){
                   CoinBaseInfo coinBaseInfo;
                   for(GlobalShowInfo showInfo:list){
                       info =  new SearchListInfo();
                       info.setTitle(showInfo.getSymbol());
                       if(defaultSymbol()){
                           info.setPrice(MoneyUtils.formatter(showInfo.getPriceCny(),false));
                           info.setValueAmount(MoneyUtils.formatter(showInfo.getMarketCapCny(),true));
                       }else{
                           info.setPrice(MoneyUtils.formatter(showInfo.getPriceUsd(),false));
                           info.setValueAmount(MoneyUtils.formatter(showInfo.getMarketCapUsd(),true));
                       }
                       info.setExchangePrice(MoneyUtils.formatter(showInfo.getPriceUsd(),false));
                       info.setRange(RangeUtil.formatter(showInfo.getPercentChange24h()));
                       info.setTargetId(showInfo.getTargetId());
                       info.setFullName(showInfo.getUsName());
                       info.setExchangeCoinName(StringUtils.isNoneBlank(showInfo.getCnName())?showInfo.getCnName():showInfo.getUsName());
                       info.setCollectId(getCollectId(userInfo,showInfo.getTargetId(),"1"));
                       coinBaseInfo = coinBaseInfoUtils.get(showInfo.getUsName());
                       if(coinBaseInfo != null){
                           info.setLogoUrl(coinBaseInfo.getLogo());
                       }
                       response.getData().getList().add(info);
                   }
               }
               break;
            case "02":
                List<MarketBaseInfo> marketBaseInfoList = this.marketBaseInfoMapper.search(request.getKeyword());
                if(CollectionUtils.isNotEmpty(marketBaseInfoList)){
                    for(MarketBaseInfo baseInfo:marketBaseInfoList){
                        info =  new SearchListInfo();
                        info.setTitle(baseInfo.getMarketName());
                        info.setTradeAmount(MoneyUtils.formatter(defaultSymbol()?baseInfo.getVol24hCny():baseInfo.getVol24hUsd(),true));
                        info.setTargetId(baseInfo.getId()+"");
                        info.setSupportTradeType(MarketSupportTypeUtils.getSupportType(baseInfo.getType()));
                        info.setLogoUrl(baseInfo.getLogo());
                        info.setCollectId(getCollectId(userInfo,baseInfo.getMarketName(),"3"));
                        response.getData().getList().add(info);
                    }
                }
                break;
            case "03":
                List<ShowInfo> showInfoList = this.showInfoMapper.search(request.getKeyword());
                if(CollectionUtils.isNotEmpty(showInfoList)){
                    MarketBaseInfo marketBaseInfo = new MarketBaseInfo();
                    List<MarketBaseInfo> targetMarketBaseList;
                    for(ShowInfo showInfo:showInfoList){
                        info =  new SearchListInfo();
                        info.setTitle(showInfo.getSymbol());
                        info.setExchangeCoinName(showInfo.getTCoin());
                        info.setTargetId(showInfo.getId()+"");
                        info.setSymbol(showInfo.getSymbol());
                        info.setMarketName(showInfo.getMarketName());
                        if(defaultSymbol()){
                            info.setTradeAmount(MoneyUtils.formatter(showInfo.getVol24hCny(),true));
                            info.setPrice(MoneyUtils.formatter(showInfo.getPriceCny(),false));
                        }else{
                            info.setTradeAmount(MoneyUtils.formatter(showInfo.getVol24hUsd(),true));
                            info.setPrice(MoneyUtils.formatter(showInfo.getPriceUsd(),false));
                        }
                        info.setExchangePrice(MoneyUtils.formatter(showInfo.getPriceUsd(),false));
                        info.setRange(RangeUtil.formatter(showInfo.getPercentChange24h()));
                        info.setCollectId(getCollectId(userInfo,showInfo.getId()+"","2"));
                        marketBaseInfo.setMarketName(showInfo.getMarketName());
                        targetMarketBaseList = this.marketBaseInfoMapper.selectList(marketBaseInfo);
                        if(!org.springframework.util.CollectionUtils.isEmpty(targetMarketBaseList)){
                            info.setHasPic(hasPic(showInfo.getSymbol(),showInfo.getTCoin(),targetMarketBaseList.get(0).getBlockccName()));
                        }else{
                            info.setHasPic("N");
                        }
                        //info.setHasPic(hasPic(showInfo.getSymbol(),showInfo.getTCoin(),showInfo.getMarketName()));
                        response.getData().getList().add(info);
                    }
                }
                break;
        }
        return response;
    }

    private String hasPic(String usName,String toCoin,String marketName){
        MarketCoinCoin cMarketCoinCoin = new MarketCoinCoin();
        cMarketCoinCoin.setFCoin(usName);
        cMarketCoinCoin.setTCoin(toCoin);
        cMarketCoinCoin.setMarketName(marketName);
        List<MarketCoinCoin> list = this.marketCoinCoinMapper.selectList(cMarketCoinCoin);
        if(org.springframework.util.CollectionUtils.isEmpty(list))
            return "N";
        if(list.get(0).getHasKline()!=null && list.get(0).getHasKline()==1){
            return "Y";
        }
        return "N";
    }

    private String getCollectId(UserInfo userInfo,String targetId,String type){
        if(userInfo != null){
            UserFollow follow = new UserFollow();
            follow.setUserName(userInfo.getTelphone());
            follow.setFCoin(targetId);
            follow.setType(type);
            List<UserFollow> list = this.userFollowMapper.selectList(follow);
            if(CollectionUtils.isNotEmpty(list)){
                return list.get(0).getId()+"";
            }
        }
        return null;
    }
}
